
import urllib2  
from weixin.api.common import getAccToken
import json
from wuxizto.settings import MEDIA_ROOT
import os


def getCustlist():
    access_token=getAccToken()
#     print access_token
    posturl = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" + access_token 
#     posturl = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=" + access_token   
    req=urllib2.urlopen(posturl)
    
    kfinfo=json.load(req);
    print type(kfinfo)
    print kfinfo
#     print len(kfinfo['kf_online_list'])
    print len(kfinfo['kf_list'])
    
#     if len(kfinfo['kf_online_list'])==0:
#         print "ok"

getCustlist()

def getCustinfo(openid):
    access_token=getAccToken()
    filepath=MEDIA_ROOT+'//userwxlogo//';
    if not os.path.exists(filepath):
        os.makedirs(filepath)
    file=filepath+openid+".jpg"
    posturl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN " %(access_token,openid);
    req=urllib2.urlopen(posturl)
    headimgurl=json.load(req)["headimgurl"]; 
    if headimgurl.startswith("http://wx.qlogo.cn"):
        open(file,'wb').write(urllib2.urlopen(headimgurl).read())

# getCustinfo("ouT3jjgzJnhP73x_uKiFia21GdSE")






